package cn.toesbieya.jxc.controller.doc;


import cn.toesbieya.jxc.enumeration.DocStatusEnum;
import cn.toesbieya.jxc.model.entity.HonorWall;
import cn.toesbieya.jxc.model.vo.HonorWallVo;
import cn.toesbieya.jxc.model.vo.R;
import cn.toesbieya.jxc.model.vo.UserVo;
import cn.toesbieya.jxc.model.vo.search.HonorWallSearch;
import cn.toesbieya.jxc.model.vo.update.DocStatusUpdate;
import cn.toesbieya.jxc.service.doc.HonorWallService;
import cn.toesbieya.jxc.util.SessionUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiao_jie
 * @since 2022-09-12
 */

@RestController
@RequestMapping("/doc/honorWall")
public class HonorWallController {

    @Resource
    private HonorWallService service;


    @GetMapping("getById")
    public R getById(@RequestParam String id) {
        if (StringUtils.isEmpty(id)) return R.fail("参数错误");
        HonorWallVo vo = service.getById(id);
        return vo == null ? R.fail("获取单据信息失败") : R.success(vo);
    }

    @GetMapping("getSubById")
    public R getSubById(@RequestParam String id) {
        if (StringUtils.isEmpty(id)) return R.fail("参数错误");
        return R.success(service.getSubById(id));
    }

    @PostMapping("search")
    public R search(@RequestBody HonorWallSearch vo) {
        return R.success(service.search(vo));
    }


    @PostMapping("add")
    public R add(@RequestBody HonorWallVo vo) {
        if (StringUtils.isEmpty(vo.getName())) {
            return R.fail("参数错误");
        }
        UserVo user = SessionUtil.get();
        vo.setCname(user.getNickName());
        vo.setStatus(DocStatusEnum.DRAFT.getCode());

        return service.add(vo);
    }


    @PostMapping("update")
    public R update(@RequestBody HonorWallVo vo) {
        String errMsg = validateUpdate(vo);
        if (errMsg != null) return R.fail(errMsg);
        return service.update(vo);
    }

    @PostMapping("commit")
    public R commit(@RequestBody HonorWallVo vo) {
        boolean isFirst = StringUtils.isEmpty(vo.getId());
        String errMsg = null;
        if (!isFirst) errMsg = validateUpdate(vo);
        if (errMsg != null) return R.fail(errMsg);

        vo.setStatus(DocStatusEnum.WAIT_VERIFY.getCode());

        if (isFirst) {
            UserVo user = SessionUtil.get();

            vo.setCname(user.getNickName());
        }

        return service.commit(vo);
    }


    @PostMapping("pass")
    public R pass(@RequestBody DocStatusUpdate vo) {
        return service.pass(vo, SessionUtil.get());
    }


    @GetMapping("del")
    public R del(@RequestParam String id) {
        if (StringUtils.isEmpty(id)) return R.fail("参数错误");
        return service.del(id);
    }

    private String validateUpdate(HonorWall main) {
        if (StringUtils.isEmpty(main.getId())
                || StringUtils.isEmpty(main.getName())
                || StringUtils.isEmpty(main.getCname())
                || StringUtils.isEmpty(main.getCtime())
                || StringUtils.isEmpty(main.getStatus())
        ) return "参数错误";
        return null;
    }

}
